import { reactive } from 'vue';
import { DateOptions, date, Screen } from 'quasar';

// 默认分页查询每页记录数
export const DEFAULT_PAGE_SIZE = Screen.lt.sm ? 10 : 15;

// 基础查询
export const useQuery = (defaultQuery = {}, initDate = false, dateOps: DateOptions = { months: -1 }) => {
  const query = reactive<BaseQuery>({ search: '' });

  if (initDate) {
    const d = new Date();
    query.endDate = date.formatDate(d, 'YYYY-MM-DD');
    query.startDate = date.formatDate(date.addToDate(d, dateOps), 'YYYY-MM-DD');
  }

  Object.assign(query, defaultQuery);
  return query;
};

// 分页查询
export const usePageQuery = (defaultQuery = {}, dateOps: boolean | DateOptions = false) => {
  const query = reactive<PageQuery>({ search: '', pageNo: 1, pageSize: DEFAULT_PAGE_SIZE, sortBy: [] });

  if (dateOps) {
    if (dateOps === true) {
      dateOps = { month: -1 };
    }
    const d = new Date();
    query.endDate = date.formatDate(d, 'YYYY-MM-DD');
    query.startDate = date.formatDate(date.addToDate(d, dateOps), 'YYYY-MM-DD');
  }

  Object.assign(query, defaultQuery);
  return query;
};
